package com.jyy.prefabricated.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.Exam;
import com.jyy.prefabricated.service.ExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 考试信息
 *
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ExamController {

    @Autowired
    private ExamService examService;


    /**
     * 新增考试
     *
     * @param exam
     * @return
     */
    @PostMapping("/exams")
    public AjaxResult insert(@Valid @RequestBody Exam exam) {
        log.info("Rest request to insert exam:{}", exam);
        Integer id = examService.insert(exam);
        Exam result = examService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改考试
     *
     * @param exam
     * @return
     */
    @PutMapping("/exams")
    public AjaxResult update(@Valid @RequestBody Exam exam) {
        log.info("Rest request to update exam:{}", exam);
        examService.update(exam);
        Exam result = examService.selectById(exam.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量更新考试状态
     *
     * @param dto
     * @return
     */
    @PutMapping("/exams-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto) {
        log.info("Rest request to batch-update news:{}", dto);
        examService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除考试
     *
     * @param dto
     * @return
     */
    @DeleteMapping("/exams-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto) {
        log.info("Rest request to batch-delete news by id:{}", dto);
        int row = examService.batchDelete(dto);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询考试详情
     *
     * @param id
     * @return
     */
    @GetMapping("/exams/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find exam by id:{}", id);
        Exam result = examService.selectById(id);


        return AjaxResult.success(result);
    }

    /**
     * 删除考试
     *
     * @param id
     * @return
     */
    @DeleteMapping("/exams/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        log.info("Rest request to delete exam by id:{}", id);
        int row = examService.delete(id);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询考试列表
     *
     * @param name       考试名称
     * @param projectId  项目id
     * @param pointId    考核点id
     * @param examStatus 考试状态
     * @param status     发布状态
     * @param startDate  考试开始时间
     * @param endDate    考试结束时间
     * @return
     */
    @GetMapping("/exams-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer projectId, @RequestParam(required = false) Integer pointId, @RequestParam(required = false) Integer examStatus, @RequestParam(required = false) Integer status, @RequestParam(required = false, defaultValue = "") String startDate, @RequestParam(required = false, defaultValue = "") String endDate, @RequestParam(required = false) Integer trainingOrganizationId) {
        log.info("Rest request to find exams by params name:{},projectId:{},pointId:{},examStatus:{},status:{},startDate:{},endDate:{}", name, projectId, pointId, startDate, endDate);
        List<Exam> result = examService.selectByParams(name, projectId, pointId, examStatus, status, startDate, endDate, trainingOrganizationId);
        return AjaxResult.success(result);
    }

    /**
     * 分页查询考试列表
     *
     * @param name       考试名称
     * @param projectId  项目id
     * @param pointId    考核点id
     * @param examStatus 考试状态
     * @param status     发布状态
     * @param startDate  考试开始时间
     * @param endDate    考试结束时间
     * @param pageable   分页信息
     * @return
     */
    @GetMapping("/exams")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer projectId, @RequestParam(required = false) Integer pointId, @RequestParam(required = false) Integer examStatus, @RequestParam(required = false) Integer status, @RequestParam(required = false, defaultValue = "") String startDate, @RequestParam(required = false, defaultValue = "") String endDate, @RequestParam(required = false) Integer trainingOrganizationId, Pageable pageable) {
        log.info("Rest request to find exams by params name:{},projectId:{},pointId:{},examStatus:{},status:{},startDate:{},endDate:{}", name, projectId, pointId, startDate, endDate);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Exam> exams = examService.selectByParams(name, projectId, pointId, examStatus, status, startDate, endDate, trainingOrganizationId);
        PageInfo<Exam> result = new PageInfo<>(exams);
        return AjaxResult.success(result);
    }
}
